<?php
class DiscountAction extends UserAction{
	
	public function index(){
		$db = M('spg_discount');
		
		$count = $db->count();
		$page=new Page($count,10);
		
		$models=$db->limit($page->firstRow.','.$page->listRows)->order('addtime desc')->select();
		
		$this->assign('models', $models);
		$this->assign('page',$page->show());
		
		$this->display();
	}
	
	public function add(){
		if(IS_POST){
			$data['title'] = $this->_post('title');
			$data['addtime'] = time();
			$data['start'] = strtotime($this->_post('start'));
			$data['end'] = strtotime($this->_post('end'));
			$data['city'] = $this->_post('city');
			
			$id = M('spg_discount')->add($data);
				
			if($id){
				$this->success('操作成功',U('Discount/index'));
			}else{
				$this->success('操作失败，请正确填写',U('Discount/index'));
			}
		}else{
			$this->assign('model',null);
			$this->display('set');
		}
		
	}
	public function edit(){
		$id = $this->_get('id');
		if(empty($id)){
			$this->error('错误的访问');
			return;
		}
		
		$db = M('spg_discount');
		
		if(IS_POST){
			$data['id'] = $id;
			
			$data['title'] = $this->_post('title');
			$data['start'] = strtotime($this->_post('start'));
			$data['end'] = strtotime($this->_post('end'));
			$data['city'] = $this->_post('city');
				
			$s = $db->save($data);
				
			if($s){
				$this->success('操作成功',U('Discount/index'));
			}else{
				$this->success('操作失败，请正确填写',U('Discount/index'));
			}
				
		}else{
			$model = $db->where(array('id'=>$id))->find();
			$this->assign('model',$model);
			$this->display('set');
		}
	}
	public function del(){
		$id = $this->_get('id');
		if(empty($id)){
			$this->error('错误的访问');
			return;
		}
		
		$id = M('spg_discount')->where(array('id'=>$id))->delete();
		$this->success('操作成功',U('Discount/index'));
	}
}